<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
	public $layout_view = 'layout/default';
	public function index($offset = 0)
	{
		$this->layout->title('Admin control panel'); // Set page title
		$this->load->model('Post_model', 'post');
		$this->load->library('pagination');
		$offset = intval($offset);
		$data = $this->post->getLatestPost(20, $offset);
		// pagination
		$paging['base_url'] = SITE_URL.'/home/index/';

		$paging['total_rows'] = $data['total'];
		$paging['per_page'] = 20;
		$paging['full_tag_open'] = '<div class="pagination"><ul>';
		$paging['full_tag_close'] = '</ul></div>';

		$paging['cur_tag_open'] = '<li class="active"><a href="#">';
		$paging['cur_tag_close'] = '</a></li>';

		$paging['num_tag_open'] = '<li>';
		$paging['num_tag_close'] = '</li>';

		$paging['prev_link'] = 'Prev';
		$paging['prev_tag_open'] = '<li>';
		$paging['prev_tag_close'] = '</li>';

		$paging['next_link'] = 'Next';
		$paging['next_tag_open'] = '<li>';
		$paging['next_tag_close'] = '</li>';

		$paging['last_link'] = 'Last';
		$paging['last_tag_open'] = '<li>';
		$paging['last_tag_close'] = '</li>';

		$paging['first_link'] = 'First';
		$paging['first_tag_open'] = '<li>';
		$paging['first_tag_close'] = '</li>';

		$this->pagination->initialize($paging);

		$data['paging'] = $this->pagination->create_links();

		$this->layout->view('home_index', $data);// Render view and layout
	}
	public function ajaxpostinfo($postId)
	{
		$this->load->model('Post_model', 'post');
		$postInfo = $this->post->getPostInfo(intval($postId));
		if(!empty($postInfo[0])){
			die(json_encode($postInfo[0]));
		}
		else{
			die(json_encode(array()));
		}
	}
	public function ajaxdeletepost($postId)
	{
		$this->load->model('Post_model', 'post');
		if($this->post->deletePost(intval($postId))){
			$return = 1;
		}
		else{
			$return = 0;
		}
		echo ($return);die;
	}
	public function ajaxupdatepost($postId)
	{
		$postId = intval($postId);
		$this->load->helper(array('input'));
		if(empty($_POST)) die(json_encode(array('result' => false)));

		array_walk($_POST, 'cleanValue');
		$arrUpdate = array(
			'post_title' 	=> $_POST['post_title'],
			'original_url' 	=> $_POST['original_url'],
			'post_image' 	=> $_POST['post_image'],
			'item_type' 	=> $_POST['item_type'],
			'source_page' 	=> $_POST['source_page'],
			'video_code' 	=> $_POST['video_code'],
			'category_id' 	=> $_POST['category_id'],
			);
		$str = $this->db->update_string('data', $arrUpdate, 'id='.$postId);
		if($this->db->query($str)){
			$response = true;
		}
		else{
			$response = false;
		}
		die(json_encode(array('result' => $response)));
	}
}